/**************************************************************************
 *  $Id : SkyConf.cpp     2011/08/01 00:00:00
 *
 *        swhors@naver.com
 **************************************************************************/

#include "config.h"

#include <stdio.h>
#include <memory.h>
#include <stdlib.h>

#include <iostream>

using namespace std;

#include "constant.h"
#include "skylistener.h"
#include "SkyConf.h"
#include "SkyConfUtil.h"

extern CSkyConf gSkyConf;

int GetConfMaxCall()
{
    return gSkyConf.mMaxCall;
}

int GetConfWaitTime()
{
    return gSkyConf.mWaitTime;
}

char *GetConfCallListPath( char * aPathPtr, int aPathLen )
{
   if ( aPathPtr != NULL )
   {
      snprintf(aPathPtr,
               aPathLen,
               "%s",
               gSkyConf.mCallListPath);
   }
   return (char*)&gSkyConf.mCallListPath;
}

char *GetConfCallOutPath( char *aPathPtr, int aPathLen )
{
   if ( aPathPtr != NULL )
   {
      snprintf( aPathPtr,
                aPathLen,
                "%s",
                gSkyConf.mCallOutPath);
   }
   return (char*)&gSkyConf.mCallOutPath;
}

char *GetConfRecordPath( char *aPathPtr, int aLen )
{
   if ( aPathPtr != NULL )
   {
      snprintf( aPathPtr,
                aLen,
                "%s",
                gSkyConf.mRecordingPath );
   }
   return (char*)&gSkyConf.mRecordingPath;
}
char *GetConfOutChanName( char *aNamePtr, int aNameLen )
{
   if ( aNamePtr != NULL )
   {
      snprintf( aNamePtr,
                aNameLen,
                "%s",
                gSkyConf.mOutChanName );
   }
   return (char *)&gSkyConf.mOutChanName;
}

char *GetConfAstUser( char *aNamePtr, int aNameLen )
{
   if ( aNamePtr != NULL )
   {
      snprintf( aNamePtr,
                aNameLen,
                "%s",
                gSkyConf.mAstUserName );
   }
   return (char *)&gSkyConf.mAstUserName;
}

void SetConfRecordPath( char *aPathPtr )
{
   if( aPathPtr != NULL )
   {
      snprintf( gSkyConf.mRecordingPath,
                MAX_PATH_LENGTH,
                "%s",
                aPathPtr);
   }
}

void SetConfCallOutPath( char *aPathPtr )
{
   if( aPathPtr != NULL )
   {
      snprintf( gSkyConf.mCallOutPath,
                MAX_PATH_LENGTH,
                "%s",
                aPathPtr);
   }
}

void SetConfCallListPath( char *aPathPtr )
{
   if( aPathPtr != NULL )
   {
      snprintf( gSkyConf.mCallListPath,
                MAX_PATH_LENGTH,
                "%s",
                aPathPtr);
   }
}

void SetConfAstUser( char *aNamePtr )
{
   if( aNamePtr != NULL )
   {
      snprintf( gSkyConf.mAstUserName,
                USERNAME_MAX_LEN,
                "%s",
                aNamePtr);
   }
}

void SetConfOutChanName( char *aNamePtr )
{
   if( aNamePtr != NULL )
   {
      snprintf( gSkyConf.mOutChanName,
                USERNAME_MAX_LEN,
                "%s",
                aNamePtr);
   }
}

void SetConfMaxCall( int aVal )
{
   gSkyConf.mMaxCall = aVal;
}

void SetConfWaitTime( int aVal )
{
   gSkyConf.mWaitTime = aVal;
}

int WriteConfWithValWithConf( char *aConfFileNamePtr,
                              char *aListPathPtr,
                              char *aCallPathPtr,
                              char *aRecordPathPtr,
                              char *aAstUserPtr,
                              char *aOutChanName,
                              int  aMaxCall,
                              int  aWaitTime )
{
   SetConfMaxCall( aMaxCall );
   SetConfCallListPath( aListPathPtr );
   SetConfCallOutPath( aCallPathPtr );
   SetConfRecordPath( aRecordPathPtr );
   SetConfAstUser( aAstUserPtr);
   SetConfOutChanName( aOutChanName );
   SetConfWaitTime( aWaitTime );
   
   return gSkyConf.WriteConf( aConfFileNamePtr );
}

int WriteConfWithVal( char *aListPathPtr,
                      char *aCallPathPtr,
                      char *aRecordPathPtr,
                      char *aAstUserPtr,
                      char *aOutChanName,
                      int  aMaxCall,
                      int  aWaitTime )
{
   SetConfMaxCall( aMaxCall );
   SetConfCallListPath( aListPathPtr );
   SetConfCallOutPath( aCallPathPtr );
   SetConfRecordPath( aRecordPathPtr );
   SetConfAstUser( aAstUserPtr );
   SetConfOutChanName( aOutChanName );
   SetConfWaitTime( aWaitTime );
   
   return gSkyConf.WriteConf();
}

int OpenConfFile( char *aConfFileName )
{
   return gSkyConf.LoadConf( aConfFileName );
}
